<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * ISP支付控制器
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */

class isppay extends general
{
    public function __construct()
    { // 公用
        parent::__construct(); // 这是必须的
    }

    
    public function orderreturn()
    {
        //header("Content-type:text/html; charset=utf-8");
        //dump($this->spArgs());      
        
        //接收返回数据
        $pMerchantCode = $this->spArgs('pMerchantCode');
        $pICPayResHashValue = $this->spArgs('pICPayResHashValue');
        
        //对pICPayRes进行base64解码,得到支付信息XML字符串
        $pICPayRes = base64_decode($this->spArgs('pICPayRes')); 
        
        //商户证书：IPS商户后台merchant.ips.com.cn下载证书内字符串。
        $pMerchantCert = trim(IPS_MIYAO);
        
        //md5签名验证
        if(md5($pICPayRes . $pMerchantCert) == $pICPayResHashValue)
        {
          //解析XML
          $xml = new DOMDocument();
          $xml->loadXML($pICPayRes);
        
          //支付结果
          $pResultCode = $xml->getElementsByTagName('pResultCode')->item(0)->nodeValue;
          
          //IPS订单号
          $pIPSOrderNum = $xml->getElementsByTagName('pIPSOrderNum')->item(0)->nodeValue;
          
            //订单号
          $order_sn = $xml->getElementsByTagName('pMerchantOrderNum')->item(0)->nodeValue;
        
            //支付金额
          $pOrderAmount = $xml->getElementsByTagName('pOrderAmount')->item(0)->nodeValue;
          
            //支付币种
          $pOrderCurrency = $xml->getElementsByTagName('pOrderCurrency')->item(0)->nodeValue;
        
            //显示金额
          $pDisplayAmount = $xml->getElementsByTagName('pDisplayAmount')->item(0)->nodeValue;
        
          //银行返回信息
          $pBankMessage = urldecode($xml->getElementsByTagName('pBankMessage')->item(0)->nodeValue);
        
          //判断交易是否成功
          if($pResultCode == 'Y')
          {

            $verified=2;
            $_SESSION['orders_id']='';
            $_SESSION['orders_sn']='';
            //setcookie('orders_id', '');
            //setcookie('orders_sn', '');
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            $order=spClass('orderModel')->find($conditions);
            if($order)
            {
   
                //存在就修改订单数据
                $order_date=date("l dS \of F Y h:i:s A",time());
                $data=array('orders_status'=>$verified,'rppay_id'=>$pIPSOrderNum,'last_modified'=>$order_date);
                spClass('orderModel')->update_order($data,$order_sn,$order['orders_id']);
                
                //订单的交易过程
                $comments='Pay notice [transactionid:'.$pIPSOrderNum.'- order_sn1:' . $order_sn .'- Currency:' . $pOrderCurrency . '-total:'.$pOrderAmount.']';
                $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'comments' =>$comments,'date_added'=>$order_date);
                spClass('orders_status_historyModel')->Create($data2);
            
                //如果为支付成功就删除购物车

                $customers_basket = spClass('customers_basketModel');
                $customers_id = $this->userarr['customers_id']; //此数据来自初始化
                $conditions = array('customers_id' => $customers_id);
                $customers_basket->delete($conditions); // 删除记录
                //这里将放置付款成功提醒邮件
                $ordermail = spClass('email_templateModel')->order_send($order_sn);//发送订单邮件
                if ($ordermail)
                {
                        $sendmailok = true;
                }
                $this->jump(spUrl('main','CheckoutSuccess'));
            }else{
                
                $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
                //echo "update status error";
                exit();
            }
            exit();
          }
          //判断延时交易录入成功
          if($pResultCode == 'S')
          {
          	//录入成功并不代表支付一定能成功，请商户在录入成功之后，根据自己的需要处理数据
          	//echo '延时交易录入成功，请等候IPS为你处理数据';
            
            $verified=1;
            
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            if(spClass('orderModel')->find($conditions))
            {
                //存在就修改订单数据
                $data=array('orders_status'=>$verified,'rppay_id'=>$pIPSOrderNum,'last_modified'=>time());
                spClass('orderModel')->update($conditions,$data);
                
                $comments='Delay trade entry success';
                //订单的交易过程
                $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'comments' =>$comments,'date_added'=>time());
                spClass('orders_status_historyModel')->Create($data2);
                
                $this->jump('checkout_success.html?code=payys');
            }
            
            exit();
          }
          //判断延时交易录入失败
          if($pResultCode == 'F')
          {
          	//echo '延时交易录入失败';
            $verified=1;
            
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            if(spClass('orderModel')->find($conditions))
            {
                //存在就修改订单数据
                $data=array('orders_status'=>$verified,'rppay_id'=>$pIPSOrderNum,'last_modified'=>time());
                spClass('orderModel')->update($conditions,$data);
                $comments='Delay trade entry failure';
                //订单的交易过程
                $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'comments' =>$comments,'date_added'=>time());
                spClass('orders_status_historyModel')->Create($data2);
                
                $this->jump('checkout_success.html?code=payerror');
            }
            
            exit();
          }
          else
          {
            //echo "交易失败:$pBankMessage";
            
            $verified=1;
            
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            if(spClass('orderModel')->find($conditions))
            {
                //存在就修改订单数据
                $data=array('orders_status'=>$verified,'rppay_id'=>$pIPSOrderNum,'last_modified'=>time());
                spClass('orderModel')->update($conditions,$data);
                $comments='Failure:'.$pBankMessage;
                //订单的交易过程
                $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'comments' =>$comments,'date_added'=>time());
                spClass('orders_status_historyModel')->Create($data2);
                
                $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
            }
            
            exit();
          }
        }
        else
        {
          //echo '签名验证失败';
          $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
          exit();
        }
    }
}